﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;



namespace SachVietDLL.BLL
{
    public class BookBLL
    {
        private SachVietDatabaseEntities db = new SachVietDatabaseEntities();

        public List<BookDetail> GetAllBooks()
        {
            var books = db.BookDetails.Include(b => b.Book_Category).Include(b => b.Publisher);
            return books.ToList();
        }

        public List<BookDetail> GetAllBooksByCategory(String categoryID)
        {
            var query = from b in db.BookDetails
                        where b.category_ID == categoryID
                        select b;
            return query.ToList();
        }


        public BookDetail GetBookByID(string bookID)
        {
            return db.BookDetails.Find(bookID);
        }

        public List<Auther> GetAutherBook(String bookID)
        {
            if(IsExist(bookID))
                return null;

            return GetBookByID(bookID).Authers.ToList();
        }

        public void Create(BookDetail bookdetail)
        {
            int cout = db.BookDetails.Count() + 1;
            string bookID = "BOOKID" + cout.ToString("0000");
            bookdetail.book_ID = bookID;
            db.BookDetails.Add(bookdetail);
            db.SaveChanges();
        }

        public bool IsExist(string bookID)
        {
            BookDetail book = GetBookByID(bookID);
            return book != null;
        }

        public void Edit(BookDetail bookdetail)
        {
            db.Entry(bookdetail).State = EntityState.Modified;
            db.SaveChanges();    
        }
        public void Delete(string id)
        {
            BookDetail bookdetail = db.BookDetails.Find(id);
            db.BookDetails.Remove(bookdetail);
            db.SaveChanges();
        }

        
    }
}